home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntinc20 / macros.h < prev    next >
C/C++ Source or Header  |  1990-03-19  |  1KB  |  59 lines

  1. /*
  2.  *    MACROS.H    commonly useful macros
  3.  */
  4.  
  5. #ifndef    _MACROS_H
  6. #define    _MACROS_H
  7.  
  8. #ifdef __GNUC__    
  9. /* with GNUC we will use safe versions, these may look like that they
  10.  * have overhead, but they do not -- trust me!
  11.  */
  12.  
  13. /* absolute value for any type of number */
  14. #define abs(a) \
  15.     ({typedef _ta = (a);  \
  16.       _ta _a = (a);     \
  17.           _a  < ((_ta)0) ? -(_a) : _a; })
  18.  
  19. /* maximum and minumum for any type of number */
  20. #define max(a,b) \
  21.     ({typedef _ta = (a), _tb = (b);  \
  22.       _ta _a = (a); _tb _b = (b);     \
  23.           _a > _b ? _a : _b; })
  24. #define min(a,b) \
  25.     ({typedef _ta = (a), _tb = (b);  \
  26.       _ta _a = (a); _tb _b = (b);     \
  27.           _a < _b ? _a : _b; })
  28.  
  29. /* swap any objects (even identically typed structs!) */
  30. /* WARNING: not safe */
  31. #define swap(a,b) \
  32.     ({typedef _ta = (a);  \
  33.       _ta _t;     \
  34.           _t = (a); (a) = (b); (b) = _t; })
  35.  
  36. #else    /* be careful !! */
  37.  
  38. /* absolute value for any type of number */
  39. #define    abs(x)        ((x)<0?(-(x)):(x))
  40.  
  41. /* maximum and minumum for any type of number */
  42. #define max(x,y)       (((x)>(y))?(x):(y))
  43. #define    min(x,y)       (((x)<(y))?(x):(y))
  44.  
  45. /* swap any objects which can be XORed */
  46. #define    swap(a,b)    ((a)=(a)^((b)=(b)^((a)=(a)^(b))))
  47.  
  48. #endif /* __GNUC__ */
  49.  
  50. /* lo and hi byte of a word */
  51. #define    lobyte(x)    (((unsigned char *)&(x))[1])
  52. #define    hibyte(x)    (((unsigned char *)&(x))[0])
  53.  
  54. /* lo and hi word of a long */
  55. #define    loword(x)    (((unsigned short *)&(x))[1])
  56. #define    hiword(x)    (((unsigned short *)&(x))[0])
  57.  
  58. #endif /* _MACROS_H */
  59.